package info.xmlprint.core.innerlink;

import java.io.Serializable;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Element;
import org.dom4j.Node;

public class InnerLink extends BaseItem implements Serializable {
	final public static String ROOT_ELEMENT = "innerlink";
	private static final long serialVersionUID = 2954005915018424555L;

	public InnerLink() {
		super(ROOT_ELEMENT);
		super.setChildrenNames(new String[] { "id", "source-element",
				"source-epath", "source-epath-index", "source-attribut",
				"target-element", "target-epath", "target-epath-index",
				"target-attribut" });
	}

	private Element addSourceTemplate(Element root) {
		String sourceKeyAttrVal = getValueOf("source-attribut").trim();
		String sourceEpath = getSourceXpath(getValueOf("source-epath"),
				sourceKeyAttrVal);

		String targetAttr = getValueOf("target-attribut");
		targetAttr = "@" + targetAttr + "=$sourceKeyAttrVal";

		String targetEpath = getTargetXpath(getValueOf("target-epath"),
				targetAttr);

		Element template = root.addElement("xsl:template");
		template.addAttribute("match", sourceEpath);

		Element var1 = template.addElement("xsl:variable");
		var1.addAttribute("name", "sourceKeyAttrVal");
		var1.addAttribute("select", "@" + sourceKeyAttrVal);

		Element copyof = template.addElement("xsl:copy-of");
		copyof.addAttribute("select", targetEpath);
		return template;
	}

	public String addXslTemplates(Element xslRoot, Element contentRoot) {
		String sourceKeyAttr = getValueOf("source-attribut").trim();
		String sourceEpath = getSourceXpath(getValueOf("source-epath"),
				sourceKeyAttr);

		String targetAttr = getValueOf("target-attribut");
		String targetEpath = getValueOf("target-epath");
		String out = "";

		@SuppressWarnings("unchecked")
		List<Node> list = contentRoot.selectNodes(sourceEpath);
		if (list.isEmpty()) {
			return out;
		}

		String sourceKeyAttrVal, targetXPath;
		Element el;
		Attribute at;
		Node target;
		addSourceTemplate(xslRoot);
		for (Node n : list) {
			el = (Element) n;
			at = el.attribute(sourceKeyAttr);
			sourceKeyAttrVal = at.getText();
			targetXPath = getTargetXpath(targetEpath, "@" + targetAttr + "='"
					+ sourceKeyAttrVal+"'");
			target = contentRoot.selectSingleNode(targetXPath);
			if (target != null) {
				Element te = xslRoot.addElement("xsl:template");
				te.addAttribute("match", targetXPath.replaceAll("^/*", ""));
			}
		}
		return out;
	}

	private String getSourceXpath(String s, String keyAttr) {
		// s = s.replaceAll("^/*", "");
		if (s.contains("[") && s.contains("]")) {
			s = s.replaceFirst("\\[", "[(");
			s = s.replaceFirst("\\]", ") and @" + keyAttr + "]");
		} else {
			s = s + "[@" + keyAttr + "]";
		}
		return s;
	}

	private String getTargetXpath(String s, String keyAttr) {
		if (s.contains("[") && s.contains("]")) {
			s = s.replaceFirst("\\[", "[(");
			s = s.replaceFirst("\\]", "");
			s += ") and " + keyAttr + "]";
		} else {
			s = s + "[" + keyAttr + "]";
		}
		return s;
	}
}
